<?php
//日本馆圣诞中奖活动
class JP_sendMailApp extends ApiApp
{
    function __construct()
    {
        $this->JP_sendMailApp();
    }

    function JP_sendMailApp()
    {
        parent::__construct();
        $this->key = "chinova2";

    }

    ///apicomponent/php/index.php?app=jp_sendmail&act=init_visitor
    function init_visitor()
    {
        $this->visitor =& env('visitor', new UserVisitor());
        $visitor_info = ecm_json_encode($this->visitor->info['user_id']);
        if (!$visitor_info) {
            echo "error";
            exit();
        }
        echo $visitor_info;
        exit();
    }

    //访问:/apicomponent/php/index.php?app=jp_sendmail&act=send_mail&encrypted_data=
    public function send_mail($encrypted_data = "")
    {
        //$encrypt_message="13146&1&一&200&1000021223456&102135658-102356468&满两百才可使用";
        //$encrypted_data = $this->encrypt($encrypt_message,$this->key);
        $encrypted_data = $_REQUEST['encrypted_data'];
        if (empty($encrypted_data)) {
            echo "the encrypted_data is empty.";
            exit();
        } else {
            $mail_message = $this->decrypt($encrypted_data, $this->key);
            $message = explode("&", $mail_message);
            $template = $message[1];
            $model_member =& m('member');
            if (!is_numeric($message[0])) {
                echo "E-mail user_d does not exist!";
                exit();
            } else {
                $user_info = $model_member->get($message[0]);
            }
            if (empty($user_info)) {
                echo "E-mail user_m does not exist!";
                exit();
            } else {
                switch ($template) {
                    case '1':
                        $template_name = "tobuyer_send_jp_coupon"; //send_touser_jp_one
                        $mail_info = array(
                            'user_name' => $user_info['user_name'],
                            'prize_num' => $message[2],
                            'coupon_value' => $message[3],
                            'coupon_sn' => $message[4],
                            'coupon_time' => $message[5],
                            'coupon_range' => $message[6]
                        );
                        break;
                    case '2':
                        $template_name = "send_touser_jp_two";
                        $mail_info = array(
                            'user_id' => $message[0],
                            'mail_title' => $message[2],
                            'content_title' => $message[3],
                            'content' => $message[4],
                            'start_time' => date("y-m-d h:i:s", $message[5]),
                            'end_time' => date("y-m-d h:i:s", $message[6])
                        );
                        break;
                    case '3':
                        $template_name = "send_touser_jp_three";
                        $mail_info = array(
                            'user_id' => $message[0],
                            'mail_title' => $message[2],
                            'content_title' => $message[3],
                            'content' => $message[4],
                            'start_time' => date("y-m-d h:i:s", $message[5]),
                            'end_time' => date("y-m-d h:i:s", $message[6])
                        );
                        break;
                }
                $send_mail = get_mail($template_name, array('mail_info' => $mail_info));
                $this->_mailto(
                    $user_info['email'],
                    addslashes($send_mail['subject']),
                    addslashes($send_mail['message'])
                );
                echo "200";
                exit();
            }
        }
    }

    /*
    function encrypt($input,$key) {
        $size = mcrypt_get_block_size('des', 'ecb');              
        $input = $this->pkcs5_pad($input, $size);
        $key = str_pad($key,24,'0');
        $td = mcrypt_module_open('des', '', 'ecb', '');           
        $iv = @mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);          
         @mcrypt_generic_init($td, $key, $iv);             
        $data = mcrypt_generic($td, $input);              
         mcrypt_generic_deinit($td);          
         mcrypt_module_close($td);             
        $data = base64_encode($data);             
        return $data;         
     }
   */
    function decrypt($encrypted, $key)
    {
        $encrypted = base64_decode($encrypted);
        $key = str_pad($key, 24, '0');
        $td = mcrypt_module_open('des', '', 'ecb', '');
        //使用MCRYPT_DES算法,cbc模式                    
        $iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
        $ks = mcrypt_enc_get_key_size($td);
        @mcrypt_generic_init($td, $key, $iv);
        //初始处理                    
        $decrypted = mdecrypt_generic($td, $encrypted);
        //解密                  
        mcrypt_generic_deinit($td);
        //结束                
        mcrypt_module_close($td);
        $y = $this->pkcs5_unpad($decrypted);
        return $y;
    }

    /*
    function pkcs5_pad ($text, $blocksize) {              
        $pad = $blocksize - (strlen($text) % $blocksize);             
        return $text . str_repeat(chr($pad), $pad);      
     }  
     */
    function pkcs5_unpad($text)
    {
        $pad = ord($text{strlen($text) - 1});
        if ($pad > strlen($text)) {
            return false;
        }
        if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) {
            return false;
        }
        return substr($text, 0, -1 * $pad);
    }
}

?>